﻿package mortal.game.view.battle
{
    import Message.Public.*;
    import com.mui.controls.*;
    import extend.language.*;
    import mortal.game.events.*;
    import mortal.game.manager.*;
    import mortal.game.manager.msgTip.*;
    import mortal.game.mvc.*;
    import mortal.game.scene3D.layer3D.utils.*;
    import mortal.game.view.common.alertwins.*;
    import mortal.mvc.core.*;

    public class BattleController extends Controller
    {

        public function BattleController()
        {
            return;
        }// end function

        override protected function initServer() : void
        {
            Dispatcher.addEventListener(EventName.BattleApply, this.onBattleApply);
            Dispatcher.addEventListener(EventName.BattleReply, this.onBattleReply);
            Dispatcher.addEventListener(EventName.BattleRequestOB, this.onBattleRequestOB);
            Dispatcher.addEventListener(EventName.ChatGuildBattle, this.onChatGuildBattle);
            Dispatcher.addEventListener(EventName.BattleGuildReply, this.onBattleGuildReply);
            NetDispatcher.addCmdListener(ServerCommand.BattleApply, this.battleApplyHandler);
            NetDispatcher.addCmdListener(ServerCommand.BattleReply, this.battleReplyHandler);
            NetDispatcher.addCmdListener(ServerCommand.BattleStart, this.battleStartHandler);
            NetDispatcher.addCmdListener(ServerCommand.BattleResult, this.battleResultHandler);
            return;
        }// end function

        private function onBattleApply(event:DataEvent) : void
        {
            GameProxy.battle.applyBattle(event.data as SEntityId);
            return;
        }// end function

        private function onBattleReply(event:DataEvent) : void
        {
            var _loc_2:* = event.data as BattleReplyObj;
            GameProxy.battle.replyBattle(_loc_2.entityId, _loc_2.isAgree);
            return;
        }// end function

        private function onBattleRequestOB(event:DataEvent) : void
        {
            GameProxy.battle.obBattle(event.data.toString());
            return;
        }// end function

        private function onChatGuildBattle(event:DataEvent) : void
        {
            GameProxy.battle.applyBattleByGuild();
            return;
        }// end function

        private function onBattleGuildReply(event:DataEvent) : void
        {
            var _loc_2:* = int(event.data.playerId);
            var _loc_3:* = String(event.data.battleId);
            GameProxy.battle.replyBattleByGuild(_loc_2, _loc_3);
            return;
        }// end function

        private function battleApplyHandler(param1:SBattleApply) : void
        {
            BattleIcon.instance.updateApplyInfo(param1);
            BattleIcon.instance.show();
            return;
        }// end function

        private function battleReplyHandler(param1:SBattleReply) : void
        {
            if (!param1.isAgree)
            {
                MsgManager.showRollTipsMsg(Language.getString(11201), MsgRollTipsType.msgRollTips3);
            }
            else
            {
                MsgManager.showRollTipsMsg(Language.getString(11202), MsgRollTipsType.msgRollTips2);
            }
            return;
        }// end function

        private function battleStartHandler(param1:SBattleStart) : void
        {
            BattleStartCDView.instance.show();
            BattleStartCDView.instance.updateBattleStart(param1);
            Dispatcher.dispatchEvent(new DataEvent(EventName.ActionIconOpenClose, false));
            return;
        }// end function

        private function battleResultHandler(param1:SBattleResult) : void
        {
            var _loc_4:SMiniPlayer = null;
            var _loc_5:SMiniPlayer = null;
            Dispatcher.dispatchEvent(new DataEvent(EventName.ActionIconOpenClose, true));
            var _loc_2:String = "";
            var _loc_3:* = EntityUtil.equal(cache.role.entityInfo.entityId, param1.playerA.entityId);
            _loc_3 = _loc_3 || EntityUtil.equal(cache.role.entityInfo.entityId, param1.playerB.entityId);
            if (EntityUtil.equal(param1.playerA.entityId, param1.winerPlayer))
            {
                _loc_4 = param1.playerA;
                _loc_5 = param1.playerB;
            }
            else if (EntityUtil.equal(param1.playerB.entityId, param1.winerPlayer))
            {
                _loc_4 = param1.playerB;
                _loc_5 = param1.playerA;
            }
            if (_loc_3)
            {
                if (_loc_4)
                {
                    if (EntityUtil.equal(cache.role.entityInfo.entityId, _loc_4.entityId))
                    {
                        _loc_2 = Language.getStringByParam(11203, _loc_5.name);
                    }
                    else
                    {
                        _loc_2 = Language.getStringByParam(11204, _loc_4.name);
                    }
                }
                else if (EntityUtil.equal(cache.role.entityInfo.entityId, param1.playerA.entityId))
                {
                    _loc_2 = Language.getStringByParam(11205, param1.playerB.name);
                }
                else
                {
                    _loc_2 = Language.getStringByParam(11205, param1.playerA.name);
                }
            }
            else if (_loc_4)
            {
                _loc_2 = Language.getStringByParam(11206, _loc_4.name, _loc_5.name);
            }
            else
            {
                _loc_2 = Language.getStringByParam(11207, param1.playerA.name, param1.playerB.name);
            }
            if (_loc_2)
            {
                MsgManager.showTaskTarget(_loc_2);
                Alert.alertWinRenderer = TimerAlertWin;
                Alert.timerOut = 5;
                Alert.show(_loc_2, "", Alert.OK, null, this.onCloseAlert);
            }
            BattleStartCDView.instance.clearObj();
            return;
        }// end function

        private function onCloseAlert(param1:int) : void
        {
            if (cache.copy.isInCopy)
            {
                GameProxy.copy.leaveCopy();
            }
            return;
        }// end function

    }
}
